//
//  HomePageController.m
//  JF_New_DYKT
//
//  Created by 陈雷 on 2017/8/22.
//  Copyright © 2017年 陈雷. All rights reserved.
//

#import "HomePageController.h"
#import "LoginController.h"
#import "HomePageCollectionV.h"
#import "SchoolNewsPaperV.h"
#import "ShoppingMall.h"
#import "HomepageHeaderV.h"
#import "HomePageCell.h"
#import "HomeSectionHeaderV.h"
#import "HomeSectionFooterV.h"


@interface HomePageController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIButton *tjBtn;//推荐
@property (weak, nonatomic) IBOutlet UIButton *xbBtn;//校报
@property (weak, nonatomic) IBOutlet UIButton *scBtn;//商城
@property (nonatomic,strong) NSArray *btnArr;//btn数组

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *topBtnV;

@property (nonatomic,strong) UIView *bottomLineV;//按钮底部的线

@property (nonatomic,strong) UIScrollView *baseScrolV;//底部滑动视图
@property (nonatomic,strong) HomePageCollectionV *homePageV;//推荐视图

@property (nonatomic,strong) SDCycleScrollView *sdScrollV;

@end

@implementation HomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [ProjHUD showMBProgressHUDOnView:self.view withGifName:@"DYKT_loading.gif"];
    
    [self configBaseUI];
    
    [self.navigationController.navigationBar setShadowImage:[JFCrazyTool imageWithColor:[UIColor colorWithHexString:@"eeeeee"]]];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"默认封面"] forBarMetrics:UIBarMetricsDefault];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"登录" style:UIBarButtonItemStylePlain target:self action:@selector(loginAction)];
    
}



- (void)loginAction{
    
    LoginController *loginVc = [[LoginController alloc]init];
    [self presentViewController:loginVc animated:YES completion:nil];
    
}

- (void)configBaseUI{
    
    self.topView.frame = CGRectMake(0, 0, KSCreenWidth, 44);
    [self.navigationController.navigationBar addSubview:self.topView];
    
    [self.topBtnV addSubview:self.bottomLineV];
    
    [self.tjBtn setTitleColor:[UIColor colorWithHexString:@"1cda9f"] forState:UIControlStateNormal];
    
    self.bottomLineV.centerX = self.tjBtn.CenterX;
    
    [self.view addSubview:self.baseScrolV];
    
    [self.baseScrolV addSubview:self.homePageV];
    
    self.btnArr = @[_tjBtn,_xbBtn,_scBtn];
    
}


- (IBAction)pushAction:(id)sender {
    
    LoginController *vc = [[LoginController alloc]init];
    
    [self presentViewController:vc animated:YES completion:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    if (!USERID) {
        LoginController *loginVc = [[LoginController alloc]init];
        [self presentViewController:loginVc animated:YES completion:nil];
    }
    
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}


#pragma mark- 分类
- (IBAction)classSelectAction:(UIButton *)sender {
    
    [self cancelAllbtnsSelectStatues:sender];
    
    
    if (sender.tag == 100) {
        NSLog(@"推荐");
    }else if (sender.tag == 200){
        NSLog(@"校报");
    }else if (sender.tag == 300){
        NSLog(@"商城");
    }
    
    [UIView animateWithDuration:.25 animations:^{
        self.bottomLineV.CenterX = sender.CenterX;
        self.baseScrolV.contentOffset = CGPointMake((sender.tag/100 - 1) * KSCreenWidth, 0);
    }];
    
}


- (void)cancelAllbtnsSelectStatues:(UIButton *)currentBtn{
    
    for (UIButton *bt in self.btnArr) {
        [bt setTitleColor:[UIColor colorWithHexString:@"070707"] forState:UIControlStateNormal];
    }
    [currentBtn setTitleColor:[UIColor colorWithHexString:@"1cda9f"] forState:UIControlStateNormal];
}

- (UIView *)bottomLineV{

    if (!_bottomLineV) {
        _bottomLineV = [[UIView alloc]initWithFrame:CGRectMake(0, 42, 36, 2)];
        _bottomLineV.backgroundColor = [UIColor colorWithHexString:@"1cda9f"];
    }
    return _bottomLineV;
}

- (UIScrollView *)baseScrolV{
    if (!_baseScrolV) {
        _baseScrolV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KSCreenWidth, KSCreenHeight - 110)];
        _baseScrolV.delegate = self;
        _baseScrolV.contentSize = CGSizeMake(KSCreenWidth*3, KSCreenHeight - 110- 64);
        _baseScrolV.pagingEnabled = YES;
        _baseScrolV.backgroundColor = [UIColor whiteColor];
        _baseScrolV.bounces = NO;
        [_baseScrolV setShowsVerticalScrollIndicator:NO];
        [_baseScrolV setShowsHorizontalScrollIndicator:NO];
    }
    return _baseScrolV;
}

- (HomePageCollectionV *)homePageV{
    if (!_homePageV) {
        UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
        layOut.sectionInset = UIEdgeInsetsMake(JFWidth(10), JFWidth(10), JFWidth(10), JFWidth(10));
        layOut.minimumLineSpacing = 10;
        layOut.minimumInteritemSpacing = 10;
        _homePageV = [[HomePageCollectionV alloc]initWithFrame:CGRectMake(0, 0, KSCreenWidth, KSCreenHeight - 110) collectionViewLayout:layOut];
        _homePageV.backgroundColor = [UIColor whiteColor];
        _homePageV.delegate = self;
        _homePageV.dataSource = self;
        
        [_homePageV registerNib:[UINib nibWithNibName:@"HomepageHeaderV" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

        [_homePageV registerNib:[UINib nibWithNibName:@"HomeSectionHeaderV" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionHeader"];
        
        [_homePageV registerNib:[UINib nibWithNibName:@"HomeSectionFooterV" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"sectionFooter"];
        
        [_homePageV registerNib:[UINib nibWithNibName:@"HomePageCell" bundle:nil] forCellWithReuseIdentifier:@"HomePageCell"];
    }
    return _homePageV;
}


- (SDCycleScrollView *)sdScrollV{
    if (!_sdScrollV) {
        _sdScrollV = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KSCreenWidth, 170) delegate:self placeholderImage:[UIImage imageNamed:@"bannerPlaceHolder.jpg"]];
        
        _sdScrollV.localizationImageNamesGroup = @[@"bannerPlaceHolder.jpg",@"bannerPlaceHolder.jpg",@"bannerPlaceHolder.jpg"];
        
        UIImageView *lineImv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 165, KSCreenWidth, 5)];
        lineImv.image = [UIImage imageNamed:@"JFbottom矩形"];
        [_sdScrollV addSubview:lineImv];
    }
    return _sdScrollV;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }
    return 5;

}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    HomePageCell *homeCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomePageCell" forIndexPath:indexPath];
    return homeCell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 3;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
    
        if (indexPath.section == 0) {
            HomepageHeaderV *headerV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
            [headerV addSubview:self.sdScrollV];
            return headerV;
        }else{
            HomeSectionHeaderV *headerV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"sectionHeader" forIndexPath:indexPath];

            return headerV;
        }
    }else{
    
        HomeSectionFooterV *footerV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"sectionFooter" forIndexPath:indexPath];
        return footerV;
        
    }

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{

    return CGSizeMake(JFWidth(170), JFWidth(140));
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return CGSizeMake(KSCreenWidth, 250);
    }else{
        return CGSizeMake(KSCreenWidth, JFWidth(60));
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{

    return CGSizeMake(KSCreenWidth, 10);
}


#pragma mark- 滑动下面后上面的也跟着变
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSLog(@"%lf",scrollView.contentOffset.x);
    
    if (scrollView.contentOffset.x == KSCreenWidth) {
        [self classSelectAction:_xbBtn];
    }else if (scrollView.contentOffset.x == KSCreenWidth*2){
        [self classSelectAction:_scBtn];
    }else{
        [self classSelectAction:_tjBtn];
    }
    
}

@end
